/// Blinky.NET - a program to reduce eye strain by reminding you to blink
/// Copyright (C) 2007  Forrest Humphrey
///
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU General Public License
/// as published by the Free Software Foundation; either version 2
/// of the License, or (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the Free Software
/// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System;
using System.Runtime.InteropServices;

namespace Blinky {
    /// <summary>
    /// This class exposes information about win32 idle time (amount of
    /// time the system has been sitting without user input) through
    /// properties.
    /// </summary>
    public sealed class IdleReporter {
        [DllImport("User32.dll")]
        private static extern bool GetLastInputInfo(ref INPUTINFO plii);

        private struct INPUTINFO {
            public uint size;
            public uint time;
        }

        /// <summary>
        /// Return the time the system has been sitting without user
        /// input in the form of milliseconds as an unsigned integer.
        /// </summary>
        /// <returns></returns>
        public static uint IdleTime {
            get {
                INPUTINFO lastInput = new INPUTINFO();
                lastInput.size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInput);
                GetLastInputInfo(ref lastInput);
                return ((uint)Environment.TickCount - lastInput.time);
            }
        }
    }
}
